package com.ruoyi.web.controller.system;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeBase;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ArtistEditLog;
import com.ruoyi.system.domain.SysArtist;
import com.ruoyi.system.domain.SysArtistProject;
import com.ruoyi.system.mapper.ArtistEditLogMapper;
import com.ruoyi.system.service.ISysArtistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * 艺人管理Controller
 * 
 * @author cooc
 * @date 2024-06-19
 */

@Api(tags = "艺人管理")
@RestController
@RequestMapping("/system/artist")
public class SysArtistController extends BaseController
{
    @Autowired
    private ISysArtistService sysArtistService;

    @Autowired
    private ArtistEditLogMapper artistEditLogMapper;

    /**
     * 查询艺人管理列表
     */

    @ApiOperation(value = "查询艺人管理列表")
    @ApiResponse(code = 200, message = "查询成功")
    @PreAuthorize("@ss.hasPermi('system:artist:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysArtist sysArtist)
    {
        startPage();
        List<SysArtist> list = sysArtistService.selectSysArtistList(sysArtist);
        return getDataTable(list);
    }

    /**
     * 导出艺人管理列表
     */

    @ApiOperation(value = "导出艺人管理列表")
    @ApiResponse(code = 200, message = "导出成功")
    @PreAuthorize("@ss.hasPermi('system:artist:export')")
    @Log(title = "艺人管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysArtist sysArtist)
    {
        List<SysArtist> list = sysArtistService.selectSysArtistList(sysArtist);
        ExcelUtil<SysArtist> util = new ExcelUtil<SysArtist>(SysArtist.class);
        util.exportExcel(response, list, "艺人管理数据");
    }

    /**
     * 获取艺人管理详细信息
     */
    @ApiOperation(value = "获取艺人管理详细信息")
    @ApiResponse(code = 200, message = "查询成功")
    @PreAuthorize("@ss.hasPermi('system:artist:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysArtistService.selectSysArtistById(id));
    }

    /**
     * 新增艺人管理
     */
    @ApiOperation(value = "新增艺人")
    @PreAuthorize("@ss.hasPermi('system:artist:add')")
    @Log(title = "艺人管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysArtist sysArtist)
    {
        sysArtist.setCreateBy(getUsername());
        return toAjax(sysArtistService.insertSysArtist(sysArtist));
    }

    /**
     * 修改艺人
     */
    @ApiOperation(value = "修改艺人")
    @ApiResponse(code = 200, message = "修改成功")
    @PreAuthorize("@ss.hasPermi('system:artist:edit')")
    @Log(title = "艺人管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysArtist sysArtist ) {
        SysArtist befInfo = sysArtistService.selectSysArtistById(sysArtist.getId());
        AjaxResult res =  toAjax(sysArtistService.updateSysArtist(sysArtist));
        ArtistEditLog log = new ArtistEditLog();
        log.setBefInfo(JSON.toJSONString(befInfo));
        log.setArtistId(sysArtist.getId());
        log.setArtistName(sysArtist.getName());
        log.setEditTime(DateUtils.getNowDate());
        log.setEditUserId(SecurityUtils.getUserId());
        log.setEditUserName(SecurityUtils.getUserNickName());
        log.setAftInfo(JSON.toJSONString(sysArtist));
        artistEditLogMapper.insertArtistEditLog(log);
        return res;
    }

    /**
     * 删除艺人
     *
     */
    @ApiOperation(value = "删除艺人")
    @ApiResponse(code = 200, message = "删除成功")
    @PreAuthorize("@ss.hasPermi('system:artist:remove')")
    @Log(title = "艺人管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysArtistService.deleteSysArtistByIds(ids));
    }


    /**
     * 头像上传
     */
    @ApiOperation(value = "头像上传")
    @ApiResponse(code = 200, message = "头像上传成功")
    @Log(title = "用户头像", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getArtistPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }
}
